local shuzhu = fk.CreateSkill {
  name = "th_shuzhu",
}

shuzhu:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and data.card and player:hasSkill(shuzhu.name) then
      local room = player.room
      local mark = player:getMark("th_shuzhu_record-turn")
      local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if use_event == nil then return false end
      if mark == 0 then
        room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          local use = e.data
          if use.card and
          table.contains(use:getAllTargets(), player) then
            mark = e.id
            room:setPlayerMark(player, "th_shuzhu_record-turn", mark)
            return true
          end
        end, Player.HistoryTurn)
      end
      return mark == use_event.id
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = shuzhu.name,
      prompt = "#th_shuzhu-invoke:::" .. data.card:getTypeString()
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3, "bottom")
    local targets = table.filter(cards, function(pid)
      return Fk:getCardById(pid).type == data.card.type
    end)
    local result, choice = room:askToChooseCardsAndChoice(player, {
      cards = targets,
      choices = {"prey"},
      skill_name = shuzhu.name,
      prompt = "#th_shuzhu-obtain:::" .. data.card:getTypeString(),
      cancel_choices = {"Cancel", "discard"},
      min_num = 1,
      max_num = 1,
      all_cards = cards
    })
    if #result ~= 0 then
      room:obtainCard(player, result, false, fk.ReasonPrey, player, shuzhu.name)
    elseif choice == "discard" then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, shuzhu.name, nil, true, player)
    end
  end,
})

Fk:loadTranslationTable{
  ["th_shuzhu"] = "鼠蛀",
  [":th_shuzhu"] = "你每回合首次成为使用牌的目标后，你可以观看牌堆底三张牌，然后可以获得其中与此牌同类型的一张牌或弃置这些牌。",

  ["#th_shuzhu-invoke"] = "鼠蛀：你可以观看牌堆底三张牌并获得一张%arg。",
  ["#th_shuzhu-obtain"] = "鼠蛀：你可以获得一张%arg，或弃置这些牌",
}

return shuzhu